package com.credit.web.basic;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.persistence.SearchDataTransform;

import com.credit.context.CreditServletContext;
import com.credit.entity.CreditLoanOrder;
import com.credit.entity.CreditProductInfo;
import com.credit.entity.CreditUserInfo;
import com.credit.exception.ErrorException;
import com.credit.service.CreditProductService;
import com.credit.util.BeanToMapUtil;
import com.credit.util.CodeValueUtil;
import com.credit.util.Constants;
import com.credit.util.ParamsConstant;
import com.credit.util.ParamsMapUtil;
import com.credit.web.basic.bean.ResBean;

@Controller
@RequestMapping("/product")
public class ProductController {
	@Autowired
	private CreditProductService productService;
	
	/**
	 * 信贷产品列表
	 * @param request
	 * @param response
	 * @param pageNumber
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public void list(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber) throws IllegalArgumentException, IllegalAccessException{
		
		Map<String, Object> searchParams = new TreeMap<String, Object>();
		String status = request.getParameter("status");
		if(!StringUtils.isEmpty(status)&&CodeValueUtil.ALL_CODE.intValue()!=Integer.valueOf(status)){
			searchParams.put("EQ_status", status);
		}
			
		Map<String, SearchDataTransform> dataTyps = new HashMap<String, SearchDataTransform>();
		

		Page<CreditProductInfo> page = productService.getListPage(searchParams, pageNumber, ParamsConstant.PAGESIZE, "createTime", dataTyps);
		
		List<CreditProductInfo> productList = page.getContent();
		List<Map<String,Object>> productListReturn = new ArrayList<Map<String,Object>>();
		for(CreditProductInfo productInfo:productList){
			Map<String,Object> resultMap = BeanToMapUtil.getMap(productInfo);
			ParamsMapUtil.replace(resultMap);
			BeanToMapUtil.removeUselessField(resultMap,ParamsConstant.PRODUCT_USELESS_FIELD_LIST);
			productListReturn.add(resultMap);
		}
		
		ResBean resBean = CreditServletContext.getResBean();
		resBean.getData().put("productList", productListReturn);
		CreditServletContext.setResBean(resBean);
	}
	
	@ResponseBody
	@RequestMapping(value = "/publish", method = RequestMethod.POST)
	public void publish(HttpServletRequest request, HttpServletResponse response) throws IllegalArgumentException, IllegalAccessException, ErrorException{
		CreditUserInfo userInfo = CreditServletContext.getUserInfo();
		
		String productName = request.getParameter("productName");
		String companyName = request.getParameter("companyName");
		String amount = request.getParameter("amount");
		String deadline = request.getParameter("deadline");
		String productType = request.getParameter("productType");		
		String monthlyPayment = request.getParameter("monthlyPayment");
		String interestRate = request.getParameter("interestRate");
		String fastLoanTime = request.getParameter("fastLoanTime");
		String requirement = request.getParameter("requirement");
		String material = request.getParameter("material");
		String desc = request.getParameter("desc");
		String logo = request.getParameter("logo");
		String city = request.getParameter("city");
		
		if(StringUtils.isEmpty(city)){
			throw new ErrorException(Constants.CITY_NOTNULL_ERROR_CODE, Constants.CITY_NOTNULL_ERROR_DESC);
		}
		
		if(StringUtils.isEmpty(productType)){
			throw new ErrorException(Constants.PRODUCTTYPE_NOTNULL_ERROR_CODE, Constants.PRODUCTTYPE_NOTNULL_ERROR_DESC);
		}
		
		if(StringUtils.isEmpty(productName)){
			throw new ErrorException(Constants.PRODUCTNAME_NOTNULL_ERROR_CODE, Constants.PRODUCTNAME_NOTNULL_ERROR_DESC);
		}
		
		if(StringUtils.isEmpty(companyName)){
			throw new ErrorException(Constants.COMPANYNAME_NOTNULL_ERROR_CODE, Constants.COMPANYNAME_NOTNULL_ERROR_DESC);
		}
		
		if(StringUtils.isEmpty(amount)){
			throw new ErrorException(Constants.AMOUNT_NOTNULL_ERROR_CODE, Constants.AMOUNT_NOTNULL_ERROR_DESC);
		}
		
		if(StringUtils.isEmpty(deadline)){
			throw new ErrorException(Constants.DEADLINE_NOTNULL_ERROR_CODE, Constants.DEADLINE_NOTNULL_ERROR_DESC);
		}
		
		if(StringUtils.isEmpty(requirement)){
			throw new ErrorException(Constants.REQUIREMENT_NOTNULL_ERROR_CODE, Constants.REQUIREMENT_NOTNULL_ERROR_DESC);
		}
		
		if(StringUtils.isEmpty(material)){
			throw new ErrorException(Constants.MATERIAL_NOTNULL_ERROR_CODE, Constants.MATERIAL_NOTNULL_ERROR_DESC);
		}
		
		CreditProductInfo productInfo = new CreditProductInfo();
		productInfo.setAmount(amount);
		productInfo.setApplicants(0);
		productInfo.setCompanyName(companyName);
		productInfo.setCoveredCity(StringUtils.isEmpty(city)?userInfo.getCurrentCity():city);
		productInfo.setDeadline(deadline);
		productInfo.setDescrible(desc);
		if(!StringUtils.isEmpty(fastLoanTime)){
			productInfo.setFastLoanTime(Integer.valueOf(fastLoanTime));
		}
		if(!StringUtils.isEmpty(interestRate)){
			productInfo.setInterestRate(interestRate);
		}
		
		if(!StringUtils.isEmpty(logo)){
			productInfo.setLogo(ParamsConstant.QINIU_URL+logo);
		}
		productInfo.setMaterial(material);
		productInfo.setMonthlyPayment(monthlyPayment);
		productInfo.setProductName(productName);
		productInfo.setProductType(Integer.valueOf(productType));
		productInfo.setRequirement(requirement);
		productInfo.setCreateTime(new Date());	
		productInfo.setUpdateTime(new Date());
		productInfo.setStatus(CodeValueUtil.PRODUCTSTATUS_CHECKPENDING);
		productInfo.setUserId(userInfo.getId());
		
/*		productInfo.setAgeMax(0);
		productInfo.setAgeMin(0);
		productInfo.setLoanAmountMax(new BigDecimal(amount));
		productInfo.setLoanAmountMin(new BigDecimal(amount));
		productInfo.setLoanDaysMax(0);
		productInfo.setLoanDaysMin(0);
		productInfo.setCreditLimit(new BigDecimal(0));
		productInfo.setPaymentMethod(0);
		productInfo.setSex(0);
		productInfo.setServiceFeeRate(0);
		productInfo.setMonthFeeRate(0);
		productInfo.setHouseLoan("ceshi");*/
		
		
		productService.addProduct(productInfo);
		
	}
	
	/**
	 * 申请下架
	 * @param request
	 * @param response
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws ErrorException 
	 */
	@ResponseBody
	@RequestMapping(value = "/undercarriage", method = RequestMethod.POST)
	public void undercarriage(HttpServletRequest request, HttpServletResponse response,@RequestParam("productId") Integer productId) throws IllegalArgumentException, IllegalAccessException, ErrorException{
		CreditUserInfo userInfo = CreditServletContext.getUserInfo();
		
		CreditProductInfo productInfo = productService.findProductById(Long.valueOf(productId));
		
		if(productInfo == null){
			throw new ErrorException(Constants.PRODUCT_NOTEXISTS_ERROR_CODE,Constants.PRODUCT_NOTEXISTS_ERROR_DESC);
		}
		
		if(productInfo.getStatus().intValue()!=CodeValueUtil.PRODUCTSTATUS_CHECK_SUCC.intValue()){
			throw new ErrorException(Constants.PRODUCT_NOCHECK_ERROR_CODE,Constants.PRODUCT_NOCHECK_ERROR_DESC);
		}
		
		if(productInfo.getUserId()!=userInfo.getId()){
			throw new ErrorException(Constants.PRODUCT_NOPUBLISH_ERROR_CODE,Constants.PRODUCT_NOPUBLISH_ERROR_DESC);
		}
		
		productInfo.setStatus(CodeValueUtil.PRODUCTSTATUS_UNDERCARRIAGE);
		
		productService.updateProduct(productInfo);
		
	}


}
